﻿using UnityEngine;
using System.Collections;

public class EnemyCharacter : Character {

	public BloodEffect bloEffect;
	
	public override void ReceiveDamage (Damaged obj)
	{
		InjuryInformation injuryInfo = obj.injury;
		if(injuryInfo.push != Vector3.zero)
		{
			Vector3 lookAtDirection = -injuryInfo.push;
			lookAtDirection.y = 0;
			transformRef.LookAt(transformRef.position + lookAtDirection);
			targetRotation = transform.rotation.eulerAngles.y;
		}
		else
		{
			if(bodyState != BodyState.Armor)
			{
				transformRef.LookAt(obj.sender.transformRef.position);
				transformRef.rotation = Quaternion.Euler(new Vector3(0, transformRef.rotation.eulerAngles.y, 0));
				targetRotation = transformRef.rotation.eulerAngles.y;
			}
		}

		if (null != bodyRenderer) {
			bodyRenderer.material.SetFloat("_TexOffset", 0.0f);
			StartCoroutine(CoRecoverNormal());
		}

		if (null != bloEffect) {
			bloEffect.Play(obj.character.transform.position, Quaternion.Euler(0, 0, 0));
		}
		Debug.Log(this + " Got Damaged To Injured State" + injuryInfo.injury + "property: " + Property);

		switch(bodyState)
		{
		case BodyState.Evading: 
		case BodyState.Normal:
		{
			if(LoseHP(injuryInfo.injury))
			{
				if(injuryInfo.fall == false)
				{
					ToState(injuredState.stateName);
				}
				else
				{
					ToState(fallState.stateName);
				}

				pushForce += injuryInfo.push;
			}
		}
		break;
		case BodyState.Defending:
		{
			if(LoseDP(injuryInfo.breakDefense))
			{
				ToState(defendState.stateName);
			}
			else
			{
				ToState(defenseBrokenState.stateName);
			}

			pushForce += injuryInfo.push;
		}
			break;
		case BodyState.Armor:
		{
			LoseHP(injuryInfo.injury);
		}
			break;
		case BodyState.Inactive:
			// shouldn't get hit when overwhelming
			break;
		}

        HitDelayAndShakeEffect effect = HitDelayAndShakeEffect.instance;
        int delayFrames = injuryInfo.receiverDelayFrames < 0? effect.defaultDelayFrames: injuryInfo.receiverDelayFrames;
                
        DelayFrame(delayFrames, true, effect.defaultShakeDistance, effect.defaultShakeFrequency);
    }

}
